package com.liurui.demo.config;

import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

//国际化解析器
public class MyLocaleResolver implements LocaleResolver {


    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获取语言中的请求参数
        String language = request.getParameter("l");//所有带l的参数都要经过这个国际化解析器

        System.out.println("DeBug==>"+language);

        Locale locale=Locale.getDefault();  //如果没有就使用默认的

        //如果请求的链接带了国际化参数
        if(!StringUtils.isEmpty(language)){
            //zh_CN
            String[] split=language.split("_");//以下划线隔开

            //国家_语言
            locale = new Locale(split[0],split[1]);
            return locale;
        }

        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
